<?php
!function_exists('adminmsg') && die('Forbidden');
require_once(D_P.'data/cache/cate.php');

$com = new Comment();
$com->doIt();

class Comment{
	var $moduledb;
	var $catedb;
	var $mid;
	/**
	 * 评论图片的路径
	 *
	 * @var string
	 */
	var $basedir =  "images/comment/";
	
	function __construct(){
		global $moduledb,$catedb,$mid;
		$this->moduledb = $moduledb;
		$this->catedb = $catedb;
		!$mid && $mid=1;
		$this->mid = $mid;
		//$this->table = 'cms_content'.$this->mid;
	}
	
	function Comment(){
		$this->__construct();
	}
	
	function doIt(){
		global $action;
		!$action && $action='show';
		switch ($action) {
			case 'show':
				$this->ShowComment();
				break;
			case 'delall':
				$this->DelComment();
				break;
			case 'delsingle':
				$this->DelSingle();
				break;
			case 'face':
				$this->ShowFace();
				break;
			case 'addface':
				$this->AddFace();
				break;
			case 'delface':
				$this->DelFace();
				break;
			case 'edit':
				$this->EditFace();
				break;
			default:
				break;
		}
	}
	
	function ShowComment(){
		global $db,$very,$basename,$page,$action;
		$rt = $db->get_one("SELECT COUNT(*) AS total FROM cms_comment WHERE mid='$this->mid'");
		$total = $rt['total'];
		$num = 20;
		if(!$page or $page<1) $page=1;
		$start = ($page-1)*$num;
		$numofpage = ceil($total/$num);
		$pages = numofpage($total,$page,$numofpage,$basename.'&mid='.$this->mid.'&');
		$rs = $db->query("SELECT c.*,t.cid,t.url,t.title FROM cms_comment c LEFT JOIN cms_contentindex t USING(tid) WHERE c.mid='$this->mid' ORDER BY c.postdate DESC LIMIT $start,$num");
		$result = array();
		while ($com = $db->fetch_array($rs)) {
			if($this->catedb[$cid]['htmlpub']){
				$com['url']=$very['htmdir'].'/'.$com['url'];
			}else{
				$com['url']='view.php?tid='.$com['tid'].'&cid='.$com['cid'];
			}
			$com['message'] = str_replace("::wind::","<br />",$com['message']);
			$com['title'] = substrs($com['title'],20);
			$com['postdate'] = get_date($com['postdate']);
			$result[] = $com;
		}
		require PrintEot('header');
		require PrintEot('ext_comment');
		adminbottom();
	}
	
	function DelComment(){
		global $db,$ids;
		if(empty($ids)) Showmsg('ext_noselect');
		foreach ($ids as $tid=>$id_a){
			$tid = intval($tid);
			$new_i = array();
			foreach ($id_a as $i){
				$new_i[] = intval($i);
			}
			$new_i = implode(',',$new_i);
			$db->update("DELETE FROM cms_comment WHERE id IN($new_i) AND tid='$tid'");
			$rows_num = mysql_affected_rows();
			$db->update("UPDATE cms_contentindex SET comnum=comnum-$rows_num WHERE tid='$tid'");
		}
		adminmsg('ext_delcommentok');
	}
	
	function DelSingle(){
		global $db;
		$tid = intval($_GET['tid']);
		$id = intval($_GET['id']);
		$db->update("DELETE FROM cms_comment WHERE id='$id'");
		$db->update("UPDATE cms_contentindex SET comnum=comnum-1 WHERE tid='$tid'");
		adminmsg('ext_delcommentok');
	}

	function ShowFace(){
		global $db,$action,$basename;
		$rs = $db->query("SELECT * FROM cms_commentface ORDER BY taxis DESC");
		$result = array();
		while ($face = $db->fetch_array($rs)) {
			$face['facepath']=$this->basedir.$face['facepath'];
			$result[] = $face;
		}
		require PrintEot('header');
		require PrintEot('ext_comment');
		adminbottom();
	}
	
	function AddFace(){
		global $db,$basename;
		$facepath = $_POST['facepath'];
		empty($facepath) && Showmsg('ext_facepathempty');
		ereg("^(\.|/)",$facepath) && showmsg('ext_facepathformat');
		if($db->get_one("SELECT * FROM cms_commentface WHERE facepath='$facepath'")){
			Showmsg('ext_facefileexists');
		}
		!file_exists(D_P.$this->basedir.$facepath) && Showmsg('ext_facepatherror');
		$faceintro = Char_cv($_POST['faceintro']);
		$db->update("INSERT INTO cms_commentface SET
			facepath='$facepath',
			faceintro='$faceintro'
		");
		require_once(R_P.'require/class_cache.php');
		Cache::writeCache('face');
		adminmsg('ext_faceaddok',"$basename&action=face");		
	}
	
	function DelFace(){
		global $db,$basename;
		$id = intval($_GET['id']);
		extract($db->get_one("SELECT facepath FROM cms_comment WHERE id='$id'"));
		unlink(D_P.$this->basedir.$facepath);
		require_once(R_P.'require/class_cache.php');
		Cache::writeCache('face');
		adminmsg('ext_faceaddok',"$basename&action=face");				
	}
	
	function EditFace(){
		global $db,$basename;
		foreach ($_POST['taxis'] as $id=>$taxis){
			if(!is_numeric($taxis)) continue;
			$faceintro = Char_cv($_POST['faceintro'][$id]);
			$db->update("UPDATE cms_commentface SET taxis='$taxis',faceintro='$faceintro' WHERE id='$id'");
		}
		require_once(R_P.'require/class_cache.php');
		Cache::writeCache('face');
		adminmsg('ext_faceeditdok',"$basename&action=face");				
	}
}
?>